/**
 * 
 */
package com.module.osr.ui.stormbringer.charviewer;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.watchable.Watchable;
import com.dalonedrau.watchable.Watcher;
import com.module.osr.stormbringer.bus.SBCharacter;
import com.module.osr.ui.stormbringer.charcreator.widgets.StepsPanel;
import com.module.osr.ui.stormbringer.charviewer.widgets.NamePanel;
import com.module.osr.ui.stormbringer.charviewer.widgets.PaperDollPanel;
import com.module.osr.ui.stormbringer.charviewer.widgets.StatsPanel;

/**
 * @author Donald
 */
public final class SBCharacterViewer extends PortraitPagePanel 
implements Watcher {
	/** the singeton instance of {@link SBCharacterViewer}. */
	private static SBCharacterViewer	instance;
	/**
	 * Gets the one and only instance of {@link SBCharacterViewer}.
	 * @return {@link SBCharacterViewer}
	 */
	public static SBCharacterViewer getInstance() {
		if (SBCharacterViewer.instance == null) {
			SBCharacterViewer.instance = new SBCharacterViewer();
		}
		return SBCharacterViewer.instance;
	}
	/** the {@link SBCharacterViewer} being viewed. */
	private SBCharacter	pc;
	/** the name panel. */
	private NamePanel	pnlName;
	/** the stats panel. */
	private StatsPanel	pnlStats;
	/** the paper doll panel. */
	private PaperDollPanel	pnlPaperDoll;
	/** Creates a new instance of {@link SBCharacterViewer}. */
	private SBCharacterViewer() {
		super(Panel.PORTRAIT_CENTER);
		try {
			init();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Initializes the viewer.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		pnlName = new NamePanel();
		super.addChild(pnlName);
		Panel middle = initMiddlePanel();
		super.addChild(middle);
		pnlPaperDoll.setDebug(true);
	}
	private Panel initMiddlePanel() {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		
		pnlStats = new StatsPanel();
		pnl.addChild(pnlStats);
		
		pnlPaperDoll = new PaperDollPanel();
		pnl.addChild(pnlPaperDoll);
		return pnl;
	}
	/**
	 * Sets the character being viewed.
	 * @param newPc the {@link SBCharacter} to set
	 */
	public void setPc(final SBCharacter newPc) {
		if (pc != null) {
			pc.removeWatcher(this);
		}
		pc = newPc;
		pc.addWatcher(this);
		watchUpdated(null);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void watchUpdated(final Watchable data) {
		pnlName.update(pc);
		pnlStats.update(pc);
		pnlPaperDoll.update(pc);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		// TODO Auto-generated method stub
		super.prepareForRendering();
	}
}
